﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace JsonEditor.Models
{
    /// <summary>
    /// 自定义属性下拉效果的类，该类主要继承StringConverter类，并重载该类的一些虚拟方法
    /// </summary>
    public class GenderItem : StringConverter
    {
        //true 启用,false 禁用
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(new string[] { "MoveMinPowerDiff", "MoveMaxPowerSum" }); //编辑下拉框中的items
        }

        //true：禁用文本编辑，false：启用文本编辑
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
    }


    //[XmlInclude(typeof(JsonSetting))]
    //[TypeConverter(typeof(ExpandableObjectConverter)), Category("参数")]
    public class JsonSetting
    {               
        #region//常用参数
        [Browsable(false)]//是否在窗口显示
        [Category("")]//对参数进行分组
        [DisplayName("FileName")]//设置参数显示名称
        [Description("Json文件名")]//参数描述
        public string FileName
        {
            get;
            set;
        }

        //[ReadOnly(false)]//是否只读
        //[DefaultValue(false)]//默认值
        [Browsable(false)]//是否在窗口显示
        [Category("Json节点属性")]//对参数进行分组
        [DisplayName("Key")]//设置参数显示名称
        [Description("Json键名称")]//参数描述
        public string JsonKey
        {
            get;
            set;
        }

        /// <summary>
        /// 扫描步长um
        /// </summary>
        [Browsable(true)]//是否在窗口显示
        [Category("Json节点属性")]//对参数进行分组
        [DisplayName("Value")]//设置参数显示名称
        [Description("键值")]//参数描述
        public double JsonValue
        {
            get;
            set;
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public JsonSetting()
        {
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName">硬件名称</param>
        /// <returns></returns>
        public JsonSetting(string fileName)
        {

        }

        /// <summary>
        /// 加载参数
        /// </summary>
        /// <param name="fileName">硬件名称</param>
        /// <returns></returns>
        public static JsonSetting Load(string fileName)
        {
            JsonSetting setting = new JsonSetting();
            string configFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName + ".xml");

            if (File.Exists(configFileName))
            {
                FileStream fs = new FileStream(configFileName, FileMode.Open);
                try
                {
                    XmlSerializer formatter = new XmlSerializer(typeof(JsonSetting));
                    setting = (JsonSetting)formatter.Deserialize(fs);
                }
                catch (SerializationException e)
                {
                    Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
                }
                finally
                {
                    fs.Close();
                }
            }

            setting.FileName = fileName;

            return setting;
        }

        /// <summary>
        /// 保存参数
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            if (string.IsNullOrEmpty(FileName))
                return false;

            bool result = true;
            string configFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FileName + "_Config.xml");
            FileStream fs = new FileStream(configFileName, FileMode.Create);
            XmlSerializer formatter = new XmlSerializer(typeof(JsonSetting));
            try
            {
                formatter.Serialize(fs, this);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                result = false;
            }
            finally
            {
                fs.Close();
            }
            return result;
        }
    }
}
